import DialogStore from ".";
import { observable, action, computed } from "mobx";
import { WeaTableNew, WeaForm } from 'comsMobx';
import u from '../../utils';
import { template as excelTemplate } from "../../apis/excelTemplate";
import { template as pathTempalte } from "../../apis/cuspath";
import condition from "../../components/list/dialog/condition";
const { TableStore } = WeaTableNew;
import { WeaLocaleProvider } from "ecCom";
const getLabel = WeaLocaleProvider.getLabel;

export default class TemplateBrowserStore extends DialogStore {
    @observable leftWidth = 480;
    @observable viewVisible = true;
    @observable tableStore = new TableStore();
    @observable current;
    @observable type = 'excel';
    @observable selectValue = 'all';
    @observable searchText = '';
    @observable condition = [];
    constructor(type) {
        super();
        this.type = type;
        if (this.type === 'excel') {
            this.template = excelTemplate;
        } else if (this.type === 'custompath') {
            this.template = pathTempalte;
        }
    }
    @computed get width() {
        if (this.viewVisible) {
            return this.leftWidth + 480;
        } else {
            return this.leftWidth;
        }
    }
    @action setTitle =  (value) =>{
         this.title = value;

    }
    @action changePageIndex = (index) => {
        this.pageIndex = index;
    }
    @action open = () => {
        this.selectValue = 'all';
        this.searchText = '';
        const _api = u.api(this.type);
        if(this.type==='excel'){
            _api('getExcelGroup', { data: {} }).then(this.setGroup).then(() => {
                this.visible = true;
                this.init()
            });
        }else {
            _api('getPathGroup', { data: {} }).then(this.setGroup).then(() => {
                this.visible = true;
                this.init()
            });
        }
    }
    @action init = () => {
        const _api = u.api(this.type);
        _api('getTemplate', { data: {} }).then(this.setDatas);
    }
    @action setGroup = (data) => {
        this.condition = [];
        this.condition.push(
            {
                "key": 'all',
                "showname": getLabel('332', '全部')
            });
        data.forEach((v) => {
            this.condition.push({ "key": v, "showname": v });
            //console.log(v);
        });
    }
    @action setDatas = (data) => {
        this.tableStore.getDatas(data.datas);
    }
    @action queryDatas = () => {
        const _api = u.api(this.type);
        _api('getTemplate', { data: { "group": this.selectValue, "name": this.searchText } }).then(this.setDatas)
    }
    @action showView = (record) => {
        this.current = record;
        this.template(record.randomFieldId).then(this.setTemplate);
    }
    @action setTemplate = (template) => {
        this.current = template;
    }
    @action toggleView = () => {
        //  this.viewVisible = !this.viewVisible;
    }
    @action changeselectValue = (value) => {
        this.selectValue = value;
        this.queryDatas();
    }
    @action onSearch = (text) => {
        this.searchText = text;
        this.queryDatas();
    }
}